package com.mypaper.list;

import com.mypaper.elem.LinkedListNode;
import com.mypaper.elem.PairElement;

public class IdleList extends OrderLinkedList{
	
	public void updataIdleNode(PairElement updataNode,float alst,float alft){
		//对于现有的空闲块，将某段置为非空闲
		LinkedListNode current;
		if(first==null){
			System.out.print("There is no idle.");
		}
		else{
			PairElement newNode=new PairElement();
			current=first;
			while(current!=null){
				if(current.info.equals(updataNode)){
					newNode.setNum(alft, ((PairElement)current.info).end);
					((PairElement)current.info).end=alst;
					current=null;
				}else{
					current=current.next;
				}
			}
			current=first;
			insertNode(newNode);
		}
	}
	
	public PairElement searchIdleSlot(float start, float end, float weight){
		//在第三阶段，已知EST与LFT后，找合适的可插入任务的空闲块
		LinkedListNode current;
		boolean found;
		current=first;
		found=false;
		while(current!=null&&!found){	
			if(current.info.properSlotMoveTo(start, end, weight)!=null)
				found=true;
			else
				current=current.next;
		}
		if(!found)
			return null;
		else{
			return (PairElement)current.info;
		}
	}
	
	public void deleteNode(PairElement deleteItem) {
		//如存在要删除的空闲块,则删除
		//该存在是全等，即首尾都相等
		LinkedListNode current;
		LinkedListNode trailCurrent;
		boolean found;
		if(first==null)
			System.out.println("Cannot delete from an empty list");
		else{
			current=first;
			trailCurrent=first;
			if(current.info.equals(deleteItem)){
				first=first.next;
				count--;
			}else{
				found=false;
				
				while(current!=null&&!found){
					if(current.info.equals(deleteItem)){
						found=true;
						if(current.info.equals(last.info)){
							last=trailCurrent;
						}
					}else{
						trailCurrent=current;
						current=current.next;
					}
				}
				if(current==null)
					System.out.println("The item to be deleted is not in the list.");					
				else{
					trailCurrent.next=current.next;
					count--;
				}
			}
		}
	}
	
	
	public void insertNode(PairElement insertItem){
		//插入一个空闲，若有相邻的空闲块，合并
		LinkedListNode current;
		LinkedListNode trailCurrent;
		LinkedListNode newNode;
		boolean found;
		newNode = new LinkedListNode();
		newNode.info=insertItem.getCopy();
		newNode.next=null;
		
		if(first==null){
			first=newNode;
			last=newNode;
			count++;
		}else{			
			current=first;		
			LinkedListNode currentNext;
			while(current!=null){
				currentNext=current.next;
				if(current.info.equalEnd(insertItem.start)){
					insertItem.start=((PairElement)current.info).start;
					deleteNode((PairElement)current.info);		
				}
				else if(current.info.equalStart(insertItem.end)){
					insertItem.end=((PairElement)current.info).end;
					deleteNode((PairElement)current.info);	
				}
				current=currentNext;
			}
			
			current=first;
			trailCurrent=first;
			newNode.info=insertItem.getCopy();
			found=false;
			while(current!=null&&!found){
				if(current.info.compareTo(insertItem)>0){
					found=true;
				}
				else{
					trailCurrent=current;
					current=current.next;
				}	
			}
			if(current==first){
				newNode.next=first;
				first=newNode;
				count++;
			}else if(current==null){
				trailCurrent.next=newNode;
				last=newNode;
				count++;
			}else{
				trailCurrent.next=newNode;
				newNode.next=current;
				count++;
			}
		}
	}
	
}
